﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace KhuyenMaiVang.HtmlHelpers
{
    public static class ImageLinkExtensions
    {
        public static HtmlString ImageLink(this HtmlHelper helper, string actionName, string imgUrl, string alt)
        {
            return ImageLink(helper, actionName, null, imgUrl, alt, null, null, null);
        }
        public static HtmlString ImageLink(this HtmlHelper helper, string actionName, string imgUrl, string alt, object routeValues)
        {
            return ImageLink(helper, actionName, null, imgUrl, alt, routeValues, null, null);
        }
        /// <param name="actionName">name of the action in controller</param>
        /// <param name="imgUrl">url of the image</param>
        /// <param name="alt">alt text of the image</param>
        /// <returns></returns>
        public static HtmlString ImageLink(this HtmlHelper helper, string actionName, string controllerName, string imgUrl, string alt)
        {
            return ImageLink(helper, actionName, controllerName, imgUrl, alt, null, null, null);
        }

        /// <summary>
        /// A Simple ActionLink Image
        /// </summary>
        /// <param name="actionName">name of the action in controller</param>
        /// <param name="imgUrl">url of the iamge</param>
        /// <param name="alt">alt text of the image</param>
        /// <returns></returns>
        public static HtmlString ImageLink(this HtmlHelper helper, string actionName, string controllerName, string imgUrl, string alt, object routeValues)
        {
            return ImageLink(helper, actionName, controllerName, imgUrl, alt, routeValues, null, null);
        }

        /// <summary>
        /// A Simple ActionLink Image
        /// </summary>
        /// <param name="actioNName">name of the action in controller</param>
        /// <param name="imgUrl">url of the image</param>
        /// <param name="alt">alt text of the image</param>
        /// <param name="linkHtmlAttributes">attributes for the link</param>
        /// <param name="imageHtmlAttributes">attributes for the image</param>
        /// <returns></returns>
        public static HtmlString ImageLink(this HtmlHelper helper, string actioNName, string controllerName, string imgUrl, string alt, object routeValues, object linkHtmlAttributes, object imageHtmlAttributes)
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
            var url = urlHelper.Action(actioNName, routeValues);
            if (controllerName != null)
            {
                url = urlHelper.Action(actioNName, controllerName, routeValues);
            }

            //Create the link
            var linkTagBuilder = new TagBuilder("a");
            linkTagBuilder.MergeAttribute("href", HttpUtility.UrlDecode(url));
            linkTagBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes));

            //Create image
            var imageTagBuilder = new TagBuilder("img");
            imageTagBuilder.MergeAttribute("src", HttpUtility.UrlDecode(urlHelper.Content(imgUrl)));
            imageTagBuilder.MergeAttribute("alt", HttpUtility.UrlDecode(urlHelper.Content(alt)));
            imageTagBuilder.MergeAttribute("title", HttpUtility.UrlDecode(urlHelper.Content(alt)));
            imageTagBuilder.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes));

            //Add image to link
            linkTagBuilder.InnerHtml = imageTagBuilder.ToString();
            return new HtmlString(linkTagBuilder.ToString());
        }
    }
}